import java.util.ArrayList;

public class CoffeePot extends Thread {
	public enum State {
		IDLE, BREWING, NOT_BREWING, COLD, POURING, OVERFLOWED, READY
	}

	State status;
	Order order;
	int tempreture;
	int capacity;
	int potNo;
	boolean debug;
	ArrayList<Addition> additions = new ArrayList<Addition>();

	public CoffeePot(int tempreture, int capacity, boolean debug, int potNo) {
		this.capacity = capacity;
		this.tempreture = tempreture;
		this.debug = debug;
		this.potNo = potNo;
		status = State.IDLE;
	}

	public String additionsToString() {
		String additions_str = "";
		int size = additions.size();
		for (int index = 0; index < size; index++) {
			additions_str += additions.get(index).getName() + ";";
		}
		return additions_str;
	}

	public void addAddition(String name, int amount) {
		Addition addition = new Addition(name, amount);
		additions.add(addition);
	}

	public void cancelOrder() {
		int size = additions.size();
		for (int additions_index = 0; additions_index <= size; additions_index++) {
			additions.get(additions_index).request(false);
		}
	}

	public Order takeOrder(Order order) {
		int size = additions.size();
		for (int index = 0; index < size; index++) {
			if (additions.get(index).getName() == order.getMilkName()) {
				additions.get(index).request(true);
				order.hasMilk(true);
			} else if (additions.get(index).getName() == order.getAlcoholName()) {
				additions.get(index).request(true);
				order.hasAlcohol(true);
			} else if (additions.get(index).getName() == order.getSyrupName()) {
				additions.get(index).request(true);
				order.hasSyrup(true);
			}
		}
		this.order = order;
		return order;
	}

	public synchronized State getPotState() {
		return status;
	}

	public synchronized void setPotState(State status) {
		this.status = status;
	}

	public void run() {
		while (true) {
			switch (this.getPotState()) {
			default: {
				try {
					Thread.sleep(500L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				break;
			}
			case BREWING: {
				while (tempreture <= 60) {
					try {
						Thread.sleep(1000L);

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					tempreture++;
					if (debug == true) {
						System.out.print("Pot: " + potNo + " tempreture: "
								+ tempreture + "\n");
					}
				}
				if (debug == true) {
					System.out.print("Pot: " + potNo + " brewed" + "\n");
				}
				this.setPotState(State.NOT_BREWING);
				break;
			}
			case POURING: {
				int amount = 0;
				int size = additions.size();
				if (!order.wantsAddition()) {
					this.setPotState(State.READY);
					break;
				}
				while ((amount <= capacity)
						&& (this.getPotState() == State.POURING)) {
					try {
						for (int index = 0; index < size; index++) {
							if (additions.get(index).isRequested()) {
								additions.get(index).use();
								amount++;
								Thread.sleep(1000L);
								System.out.print(amount + "\n");
							}
							if (amount == capacity) {
								if (debug == true) {
									System.out.print("Pot: " + potNo
											+ " has overflowed" + "\n");
								}
								this.setPotState(State.OVERFLOWED);
							}
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				break;
			}
			case READY: {
				while (tempreture >= 35 && this.getPotState() == State.READY) {
					try {
						Thread.sleep(1000L);

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					tempreture--;
					if (debug == true) {
						System.out.print("Pot: " + potNo + " tempreture: "
								+ tempreture + "\n");
					}
					if (tempreture == 35) {
						if (debug == true) {
							System.out.print("Pot: " + potNo + " is cold"
									+ "\n");
						}
						this.setPotState(State.COLD);
					}
				}
				break;
			}
			}
		}
	}
}
